home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Programming / Programming Tools / Turbo Pascal / Utilities / FILEUT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-06-16  |  14.0 KB  |  455 lines  |  [TEXT/ttxt]

  1. UNIT FileUT(56); {              Version 2.1a                     87/03/16
  2.  
  3.   Some file-related routines to make life easier for the MacTURBO programmer.
  4.   
  5.   Features:
  6.   
  7.      1) Use the standard file dialogs to open & close TURBO files.
  8.  
  9.      2) Launch applications from your TURBO program.
  10.      
  11.      3) Some procedures to make translation from TURBO 3.0 a little easier.
  12.  
  13.  
  14.   History:
  15.   
  16.     86/12/31 - Origional version of this file.
  17.     
  18.     87/03/14 - Version 2.0. Added SFDialogs stuff. (Mike Babulic, CIS:72037,314)
  19.     
  20.     87/03/15 - Version 2.1. To change the File Type & Creator use the
  21.                standard MacTURBO Pascal variables:
  22.                   FileType, FileCreator,
  23.                   TextType, TextCreator : packed array[1..4] of char;
  24. }
  25.  
  26. INTERFACE
  27.  
  28. {$U-}
  29.  
  30.   USES MemTypes,QuickDraw,OSIntf,ToolIntf,PackIntf,PasInOut;
  31.   
  32. {------------------------------------------------------------------------------}
  33.  
  34. const
  35.   DefaultBlockSize = 512;      { Standard block size on Macintosh }
  36.  
  37. type
  38.   FileBuf = packed array[0..MaxInt] of char;
  39.   FileBufferPtr = ^FileBuf;
  40.   FileProcPtr = ^Integer;
  41.   FileRec = record    { Internal format of a Turbo file variable }
  42.               FInpFlag : boolean;
  43.               FOutFlag : boolean;
  44.               FRefNum  : integer;  { Reference number is used for }
  45.               FVRefNum : integer;       { Mac File Manager calls }
  46.               FBufSize : integer;
  47.               FBufPos  : integer;
  48.               FBufEnd  : integer;
  49.               FBuffer  : FileBufferPtr;
  50.               FInOutProc : FileProcPtr;
  51.             end;
  52.   FileRecPtr = ^FileRec;
  53.  
  54. {----------------------------------------------------------------------------}
  55.  
  56.  
  57.  
  58. const
  59.   TextFile = -1;        {If the "SizeOf" parameter in the routines below is
  60.                            equal to this value. The file will be opened as a
  61.                            TEXT file}
  62.                     
  63. type 
  64.   
  65.   UntypedFile = file of byte;  { Block operations are on untyped, }
  66.  
  67. var
  68.   FileBlockSize : LongInt;        {Bytes in a given block }
  69.   FileErr : OSErr;                {see File Manager in Inside Mac}
  70.   
  71.   SFDialog : record               {Stuff used by Standard File Dialog}
  72.     where : Point;                   {Where to place the SFDialogs}
  73.     prompt: str255;                  {Used by the SFDialogs}
  74.     InpFileTypes : string[16];       {SFGetFile Search Types}
  75.     r : SFReply;                     {Results from StFDialog goes Here}
  76.     end;
  77.  
  78.  
  79.  
  80.                     
  81. FUNCTION SFGetReset(var f; SizeOf:LongInt; fName: str255):Boolean;
  82.   {Use the SFGetFile dialog to Open & Reset the file "f"
  83.      If "SizeOf" is "TextFile" then "f" is "Reset(Text(f))"
  84.      otherwise "SizeOf" contains the length of a file record and
  85.      f is "Reset(UntypedFile(f))"                   }  
  86.  
  87. FUNCTION SFGetRewrite(var f; SizeOf:LongInt; fName: str255):Boolean;
  88.   {Use the SFGetFile dialog to Open & Rewrite the file "f".
  89.      If "SizeOf" is "TextFile" then "f" is "Rewrite(Text(f))"
  90.      otherwise "SizeOf" contains the length of a file record and
  91.      f is "Rewrite(UntypedFile(f))"                 } 
  92.  
  93. FUNCTION SFGetCreate(var f; SizeOf:LongInt; fName: str255):Boolean;
  94.   {Use the SFGetFile dialog to Create a NEW file "f". 
  95.      If "SizeOf" is "TextFile" then "f" is "Rewrite(Text(f))"
  96.      otherwise "SizeOf" contains the length of a file record and
  97.      f is "Rewrite(UntypedFile(f))"                 }  
  98.  
  99. FUNCTION SFGetAppend(var f:Text; fName: str255):Boolean;
  100.   {Use the SFGetFile dialog to open "f" and go to the END OF the FILE
  101.    so as to APPEND any "writes" to the end of the file "f"}
  102.  
  103. FUNCTION SFGetDelete(theFile: str255):Boolean;
  104.   {Use the SFGetFile dialog to Delete the file "f"  } 
  105.  
  106. FUNCTION SFGetLaunch:boolean;
  107.   {Use the SFGetFile dialog to LAUNCH another application}
  108.   
  109.  
  110.  
  111. FUNCTION SFPutRewrite(var f; SizeOf:LongInt; fName: str255):Boolean;
  112.     {Use the SFPutFile dialog to Open & Rewrite the file "f"
  113.          If "SizeOf" is "TextFile" then "f" is "Rewrite(Text(f))"
  114.          otherwise "SizeOf" contains the length of a file record and
  115.          f is "Rewrite(UntypedFile(f))"  }  
  116.          
  117.          
  118. FUNCTION SFPutCreate(var f; SizeOf:LongInt; fName: str255):Boolean;
  119.     {Use the SFPutFile dialog to Create a NEW file "f".
  120.          If "SizeOf" is "TextFile" then "f" is "Rewrite(Text(f))"
  121.          otherwise "SizeOf" contains the length of a file record and
  122.          f is "Rewrite(UntypedFile(f))"  }  
  123.          
  124.  
  125.  
  126.  
  127. FUNCTION GetFile(var f; var theFile:str255):Boolean;
  128.   {Use the SFGetFile dialog -- nb. f.fVrefNum contains the volume #}
  129.  
  130. FUNCTION PutFile(var f; var theFile:str255):Boolean;
  131.   {Use the SFPutFile dialog -- nb. f.fVrefNum contains the volume #}
  132.          
  133.  
  134.  
  135.  
  136. PROCEDURE Str2Types(TypeList:str255; MaxCount:integer;
  137.                     VAR count:integer; VAR tl:SFTypeList);
  138.     {Translate the string into an SFTypelist}
  139.  
  140.  
  141.  
  142.  
  143. PROCEDURE Append(var f:Text; fName: str255);
  144.     { For Turbo 3.0 Compatability: Append Text file output to the end of the
  145.       named file}
  146.  
  147. PROCEDURE Execute(progName: str255);
  148.     { For Turbo 3.0 Compatability: Execute the named program}
  149.  
  150. PROCEDURE BlockRead(var f:UntypedFile;
  151.                     var Buf;
  152.                         NumBlocks: LongInt;
  153.                     var BlocksRead: LongInt);
  154.     { For Turbo 3.0 Compatability: Reads NumBlocks of Data from f into Buf.
  155.             BlocksRead is the number of blocks actually read.}
  156.  
  157. PROCEDURE BlockWrite(var f:UntypedFile;
  158.                      var Buf;
  159.                          NumBlocks: LongInt;
  160.                      var BlocksWritten: LongInt);
  161.     { For Turbo 3.0 Compatability: Reads NumBlocks of Data from f into Buf.
  162.             BlocksWritten is the number of blocks actually read.}
  163.  
  164.  
  165.  
  166.  
  167. IMPLEMENTATION
  168.  
  169.  procedure Str2Types{TypeList:str255; MaxCount:integer;
  170.                       VAR count:integer; VAR tl:SFTypeList);        };
  171.   VAR i,j,k: INTEGER;
  172.   begin
  173.     count :=Length(TypeList) DIV 4;
  174.     if count>MaxCount then count := MaxCount;
  175.     k := 0;
  176.     for i := 0 to count-1 do begin
  177.       for j := 1 to 4 do begin
  178.         tl[i][j] := TypeList[j+k];
  179.       end;
  180.       k := k + 4;
  181.     end;
  182.   end;
  183.                     
  184. (***************************************************************************)
  185.    
  186. FUNCTION GetFile{var f; var theFile:str255):boolean;        };
  187.  var where : point;
  188.       count: Integer;
  189.       tl   : SFTypeList;
  190.   begin with FileRec(f) do with SFDialog do begin
  191.     FileErr := noErr;
  192.     Str2Types(SFDialog.InpFileTypes,4,count,tl); 
  193.     SFGetFile(SFDialog.where,theFile,NIL,count,tl,NIL,r);
  194.     GetFile := r.good;
  195.     IF r.good THEN with r do begin
  196.       FileErr := SetVol(NIL,vRefNum);
  197.       FvRefNum := vRefNum;
  198.       theFile  := fName;
  199.     END;
  200.   end end;
  201.  
  202. FUNCTION PutFile{var f; var theFile:str255):boolean;        };
  203.  var where : point;
  204.       count: Integer;
  205.       r    : SFReply;
  206.   begin with FileRec(f) do with SFDialog do begin
  207.     FileErr := noErr;
  208.     SFPutFile(SFDialog.where,SFDialog.prompt,theFile,NIL,r);
  209.     PutFile := r.good;
  210.     IF r.good THEN with r do begin
  211.       FileErr := SetVol(NIL,vRefNum);
  212.       FvRefNum := vRefNum;
  213.       theFile  := fName;
  214.     END;
  215.   end end;
  216. {$I-} 
  217. FUNCTION ResetFile(var f; SizeOf:LongInt):Boolean;
  218.   begin with FileRec(f) do with SFDialog do with r do begin
  219.     if SizeOf = TextFile then
  220.       reset(Text(f),fName,FileBlockSize)
  221.     else begin
  222.       reset(UntypedFile(f),fName);
  223.       FBufSize := SizeOf;
  224.     end;
  225.     FileErr := IOResult;
  226.     ResetFile := (FileErr = noErr); 
  227.   end end; 
  228.  
  229. FUNCTION RewriteFile(var f; SizeOf:LongInt; Nuke:boolean):Boolean;
  230.   PROCEDURE RewriteType;
  231.     begin
  232.       if SizeOf = TextFile then
  233.         rewrite(Text(f),SFDialog.r.fName,FileBlockSize)
  234.       else begin
  235.         rewrite(UntypedFile(f),SFDialog.r.fName);
  236.         with FileRec(f) do
  237.           FBufSize := SizeOf;
  238.       end;
  239.       FileErr := IOResult;
  240.     end;
  241.   begin 
  242.     if Nuke then begin
  243.       FileErr := FSDelete(SFDialog.r.fName,0);
  244.       if (FileErr=NoErr) or (FileErr=FNFErr) then 
  245.         RewriteType
  246.       end
  247.     else begin
  248.       RewriteType;
  249.     end;
  250.     RewriteFile := FileErr = noErr; 
  251.   end;
  252. {$I+}  
  253. (**************************************************************************)
  254.                   
  255. FUNCTION SFGetReset{var f; SizeOf:LongInt; fName:str255):Boolean;         };
  256.   var   ok: boolean;
  257.   begin
  258.     SFGetReset := FALSE;
  259.     if GetFile(f,fName) then
  260.       SFGetReset := ResetFile(f,SizeOf);
  261.   end;
  262.   
  263. FUNCTION SFGetRewrite{var f; SizeOf:LongInt; fName:str255):Boolean;        };
  264.   begin
  265.     SFGetRewrite := FALSE;
  266.     if GetFile(f,fName) then
  267.       SFGetRewrite := RewriteFile(f,SizeOf,FALSE);
  268.   end;
  269.   
  270. FUNCTION SFGetCreate{var f; SizeOf:LongInt; fName:str255):Boolean;        };
  271.   begin
  272.     SFGetCreate := FALSE;
  273.     if GetFile(f,fName) then
  274.       SFGetCreate := RewriteFile(f,SizeOf,TRUE);
  275.   end;
  276.  
  277. FUNCTION SFGetDelete{theFile:str255):Boolean;            };
  278.   type strPtr = ^str255;
  279.   var f: FileRec;
  280.   begin with SFDialog do with r do begin
  281.     SFGetDelete := FALSE; 
  282.     if GetFile(f,theFile) then begin
  283.       FileErr := FSDelete(fName,f.FvRefNum);
  284.       SFGetDelete := (FileErr = NoErr);
  285.     end;
  286.   end end;
  287.  
  288. PROCEDURE Append{f:Text; fName: str255);                 };
  289.   {Open Text file so that "writes" will append to its end}
  290.   begin
  291.     reset(f,fName);
  292.     with FileRec(f) do begin
  293.       FInpFlag := FALSE;
  294.       FOutFlag := TRUE;
  295.       FileErr  := SetFPos(FRefNum,FsFromLEOF,1);
  296.     end;
  297.   end;
  298.   
  299. FUNCTION SFGetAppend{var f:Text; fName: str255):Boolean;            };
  300.   var ok : boolean;
  301.   begin
  302.     ok := SFGetReset(f,TextFile,fName);
  303.     SFGetAppend := ok;
  304.     if ok then
  305.       with FileRec(f) do begin
  306.         FInpFlag := FALSE;
  307.         FOutFlag := TRUE;
  308.         FileErr  := SetFPos(FRefNum,FsFromLEOF,1);
  309.         SFGetAppend := (FileErr=EOFErr);
  310.       end;
  311.   end;       
  312.  
  313. FUNCTION SFPutRewrite{var f; SizeOf:LongInt; fName:str255):Boolean;        };
  314.   begin
  315.     SFPutRewrite := FALSE; 
  316.     if PutFile(f,fName) then
  317.       SFPutRewrite := RewriteFile(f,SizeOf,FALSE);
  318.   end;       
  319.  
  320. FUNCTION SFPutCreate{var f; SizeOf:LongInt; fName:str255):Boolean;        };
  321.   begin
  322.     SFPutCreate := FALSE; 
  323.     if PutFile(f,fName) then
  324.       SFPutCreate := RewriteFile(f,SizeOf,TRUE);
  325.   end;
  326.  
  327. (****************************************************************************)
  328.   
  329. TYPE LaunchRec = record             {Used by the Inline routine below }
  330.        ProgramName  : ^Str255;      {  see "Inside Macintosh II" pp.59-60}
  331.        SoundBuffer  : integer;
  332.        end;
  333.        
  334. PROCEDURE LaunchIt(var L:LaunchRec); 
  335.  inline
  336.     $205F,      { MOVE.L    (SP)+,A0    } { move parameter to A0 }
  337.     $A9F2;      { _LAUNCH               }
  338.   
  339. PROCEDURE Execute{progName: str255);                     };
  340.   {Launch the program progName}
  341.   var L: LaunchRec;
  342.       F: file of Byte;
  343.   begin
  344.     Reset(F,ProgName); {The program will crash here if the file doesn't exist}
  345.     Close(F);
  346.     with L do begin
  347.       SoundBuffer := 0;
  348.       { uses Main sound & screen buffers. If you want the current buffer,
  349.         you need to write inline to get it from the variable CurPageOption.}
  350.       ProgramName := @progName;
  351.     end;
  352.     LaunchIt(L);
  353.   end;
  354.  
  355. FUNCTION SFGetLaunch{:boolean;                          };
  356.   {Launch a program using the SFGetFile Dialog}
  357.   var L: LaunchRec;
  358.       types: SFTypeList;
  359.       ok: boolean;
  360.   begin with SFDialog do with r do begin
  361.     types[0] := 'APPL';
  362.     SFGetFile(where,'',NIL,1,types,NIL,r);
  363.     IF Good THEN begin
  364.       Good := (FileErr = NoErr);
  365.       FileErr := SetVol(NIL,vRefNum);
  366.       if Good then begin
  367.         with L do begin
  368.           SoundBuffer := 0;
  369.           { uses Main sound & screen buffers. If you want the current buffer,
  370.             you need to write inline to get it from the variable CurPageOption.}
  371.           ProgramName := @r.fName;
  372.         end;
  373.         LaunchIt(L);
  374.       end;
  375.     end;
  376.     SFGetLaunch := Good;
  377.   end end;
  378.  
  379. {
  380.   procedure WriteFileRec(var f:UntypedFile);
  381.     begin with FileRec(f) do begin
  382.       writeln('FinpFlag = ',FinpFlag);
  383.       writeln('FOutFlag = ',FOutFlag);
  384.       writeln('FBufSize = ',FBufSize);
  385.       writeln('FBufPos  = ',FBufPos);
  386.       writeln('FBufEnd  = ',FBufEnd);
  387.       writeln('FBuffer  = ',LongInt(FBuffer));
  388.       writeln('FInOutProc = ',LongInt(FInOutProc));
  389.     end end;
  390.  }
  391.  
  392. (*****************************************************************************)
  393.  
  394. PROCEDURE BlockRead{var f:UntypedFile;
  395.                     var Buf;
  396.                         NumBlocks: LongInt;
  397.                     var BlocksRead: LongInt);
  398.       For Turbo 3.0 Compatability: Reads NumBlocks of Data from f into Buf.
  399.             BlocksRead is the number of blocks actually read.};
  400.   begin with FileRec(f) do begin
  401.     BlocksRead := NumBlocks * FBufSize;                     {Convert to # of Bytes}
  402.     if BlocksRead > 0 then begin
  403.       FileErr := FSRead(FRefNum, BlocksRead, @Buf);
  404.       BlocksRead := (BlocksRead+fBufSize-1) DIV FBufSize;   {Convert to # of Blocks}
  405.     end;
  406.   end end;
  407.  
  408. PROCEDURE BlockWrite{var f:UntypedFile;
  409.                     var Buf;
  410.                         NumBlocks: LongInt;
  411.                     var BlocksRead: LongInt);
  412.       For Turbo 3.0 Compatability: Reads NumBlocks of Data from f into Buf.
  413.             BlocksRead is the number of blocks actually read.};
  414.   begin with FileRec(f) do begin
  415.     BlocksWritten := NumBlocks * FBufSize;                      {Convert to # of Bytes}
  416.     if BlocksWritten > 0 then begin
  417.       FileErr := FSWrite(FRefNum, BlocksWritten, @Buf);
  418.       BlocksWritten := (BlocksWritten+fBufSize-1) DIV FBufSize; {Convert to # of Blocks}
  419.     end;
  420.   end end;
  421.  
  422. (*****************************************************************************)
  423.  
  424. begin   
  425.   FileBlockSize := DefaultBlockSize;
  426.   FileErr := noErr;
  427.   with SFDialog do begin
  428.     SetPt(where,100,100);
  429.     prompt := 'Save file as:';
  430.     InpFileTypes := 'TEXT';
  431.   end;
  432. end.
  433.  
  434. (****************** Example Program:**************************
  435. program test;
  436.  
  437.   {$U FileUT}
  438.   USES MemTypes,QuickDraw,OSIntf,ToolIntf,PackIntf,FileUT;
  439.  
  440. var f : text;
  441.     s : str255;
  442.  
  443. begin
  444.   if SFGetReset(f,TextFile,'') then begin
  445.     while not eof(f) do begin
  446.       readln(f,s);
  447.       writeln(s);
  448.     end;
  449.     Close(f);
  450.     end
  451.   else
  452.     writeln('FileError =',FileErr);
  453.   repeat until keypressed;
  454. end.
  455. *)